/*
* Pager (Refactored)@VERSION
*
* Copyright XXXX, COPYRIGHT HOLDERS
*
* Depends:
* jquery.pajinate.min.js
*/

var API = API || {};

API.x_pager = {
    PagerManager: function(results_container, item_container_id, pager_id, search_list) {
    	var parent = this;
    	
    	var results_container = results_container;
    	var item_container_id = item_container_id;
    	var search_list = search_list;
    	this.pager_id = pager_id;
    	
    	this.effects_manager = new API.x_effects.EffectsManager();
    	this.effects_manager.Init(search_list, this.pager_id);

    	$.fn.preBind = function(type, data, fn) {
    		this.each(function() {
    		var $this = $(this);
    		 
    		$this.bind(type, data, fn);
    		 
    		var currentBindings = $._data($this[0], 'events');
    		if ($.isArray(currentBindings[type])) {
    		currentBindings[type].unshift(currentBindings[type].pop());
    		}
    		});
    		 
    		return this;
    	};

    	this.Init = function() {
    		
            this.afterPageLinkHandler = function(page_num, init) {
            	if(init == undefined){
//                	console.log("after page:", page_num);
//                	var start = new Date().getMilliseconds();
                	parent.effects_manager.afterEffects2();
//                	var end = new Date().getTime();
//                	var time = end - start;
//                	console.log('Execution time for after page: ', time);           		
            	}
            };
            $(results_container).pajinate({
                items_per_page: 10,
                item_container_id: item_container_id,
                wrap_around: false,
                nav_label_first: '<<<',
                nav_label_last: '>>>',
                nav_label_prev: '<<',
                nav_label_next: '>>',
                num_page_links_to_display: 10,
                show_first_last: false,
//                setStartRenderingCb: parent.setStartRenderingObj,
                onPageDisplayed: parent.afterPageLinkHandler
//                    abort_on_small_lists: true
            });
            
			// On each reload reset the pagination on click handler
            $( ".page_link" ).preBind("click", function() {
				if(is_filtering === true){
					return false;
				}
            	parent.effects_manager.preEffects2();
            	
            	console.log("from page");
//            	var start = new Date().getMilliseconds();

            	

//            	var end = new Date().getTime();
//            	var time = end - start;
//            	console.log('Execution time for from page: ', time);
			});
            $( ".previous_link" ).preBind("click", function() {
            	console.log("previous_link");
				if(is_filtering === true){
					return false;
				}
            	parent.effects_manager.preEffects2();
            	
//            	var start = new Date().getMilliseconds();
            	
            	
            	
//            	var end = new Date().getTime();
//            	var time = end - start;
//            	console.log('Execution time for from page: ', time);
            });
            $( ".next_link" ).preBind("click", function() {
            	console.log("next_link");
            	if(is_filtering === true){
            		return false;
            	}
            	parent.effects_manager.preEffects2();
            	
//            	var start = new Date().getMilliseconds();
            	
            	
            	
//            	var end = new Date().getTime();
//            	var time = end - start;
//            	console.log('Execution time for from page: ', time);
            });

        };
        this.setStartRenderingObj = function(func, page_number) {
//        	console.log("function: ", func);
        	parent.startRenderingObj = func;
        	parent.page_number = page_number;
		};
        
		this.startRenderingCb = function() {
//			console.log("startRenderingCb cb:", parent.startRenderingObj);
			setTimeout(function() {
//				try{
//					parent.startRenderingObj(parent.page_number);
////						console.log("startRenderingCb cb function");
//					
//		
//				}catch(e){
//					console.log(e);
//				}
//				parent.effects_manager.afterAffects2;
			}, 1000);
			
				
			};

		this.getContainerId = function() {
			return parent.pager_id;
		};
    }
};
